﻿using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProTrack.Report
{
    /// <summary>
    /// 公共基类报表服务
    /// </summary>
    public class BaseReport
    {
        /// <summary>
        /// 公共导出数据模板【此数据模板不适用与定制的模板，只是按照配置的列输出所有的数据】
        /// </summary>
        /// <typeparam name="T">具体类型</typeparam>
        /// <param name="list">泛型数据集合</param>
        /// <param name="Title">工作簿标签名</param>
        /// <param name="TitleArray">显示的列</param>
        /// <param name="ColumnWidth">默认列宽</param>
        /// <param name="isAddNo">是否需要序号</param>
        /// <param name="showTitle">是否需要显示标题</param>
        /// <returns></returns>
        public static byte [] Export<T>(IEnumerable<T> list,string Title, ReportDomain[] TitleArray,int ColumnWidth = 30,bool isAddNo=true,bool showTitle=false) where T : class
        {
            IWorkbook _wokebook = new HSSFWorkbook();
            
            var sheet = _wokebook.CreateSheet(Title);
            sheet.DefaultColumnWidth = ColumnWidth;

            //其他行样式
            var cellStyleContent = WorkBookHelper.CreateCellStyle(_wokebook, HorizontalAlignment.Center, VerticalAlignment.Center, true, 10, false);
            int index = 0;

            if (showTitle)
            {
                //设置第一行
                var rowTitle = sheet.CreateRow(0);
                var cellTitle = rowTitle.CreateCell(0);
                //合并第一行
                var rangeCell = new CellRangeAddress(0, 0, 0, TitleArray.Length-1);
                sheet.AddMergedRegion(rangeCell);
                cellTitle.SetCellValue(Title);

                //单元格样式
                var cellStyle = WorkBookHelper.CreateCellStyle(_wokebook, HorizontalAlignment.Center, VerticalAlignment.Center, false, 24, true);
                cellTitle.CellStyle = cellStyle;

                index++;
            }

            //标题
            {
                var rows = sheet.CreateRow(index);
                rows.Height = 20 * 20;
                for (int i = 0; i < TitleArray.Length; i++)
                {
                    string CellTitles = TitleArray[i].Title;

                    var row_cell = rows.CreateCell(i);
                    row_cell.SetCellValue(CellTitles);
                    row_cell.CellStyle = cellStyleContent;
                }
                index++;
            }
            //数据部分
            {
                int No = 1;
                foreach (var item in list)
                {
                    var rows = sheet.CreateRow(index);
                    rows.Height = 20 * 20;

                    var types = item.GetType();

                   
                    //按国定的字段进行输出显示
                    for (int i = 0; i < TitleArray.Length; i++)
                    {
                        var row_cell = rows.CreateCell(i);
                        row_cell.CellStyle = cellStyleContent;

                        if (isAddNo && TitleArray[i].Fileld.Equals("No"))
                        {
                            row_cell.SetCellValue(No.ToString());
                            continue;
                        }
                       
                        var PropertiesInfoModel =types.GetProperties().FirstOrDefault(x => x.Name.Equals(TitleArray[i].Fileld));
                        if (PropertiesInfoModel != null)
                        {
                            var vals = PropertiesInfoModel.GetValue(item) ?? "";
                            //判断条件
                            if (TitleArray[i].IsTemplate)
                            {
                                if (TitleArray[i].Where!=null&& vals.Equals(TitleArray[i].Where))
                                {
                                    row_cell.SetCellValue(TitleArray[i].ValueTrue);
                                }else
                                {
                                    row_cell.SetCellValue(TitleArray[i].ValueFalse);
                                }
                            }
                            else
                            row_cell.SetCellValue(vals.ToString());
                        }
                        else
                        {
                            row_cell.SetCellValue(string.Empty);
                        }

                    }

                    index++;
                    No++;
                }

            }
            return WorkBookHelper.ToExcelByteArray(_wokebook);
        }

        
    }
}
